package cn.flying.cloud.task.controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.task.entity.TaskJobLog;
import cn.flying.cloud.task.service.TaskJobLogService;

/**
 * 定时任务执行日志 前端控制器
 *
 * @author: admin
 * @date: 2025-04-27 16:34:20
 * @version: 1.0
 */
@Controller
@RequestMapping("/task/job/log")
public class TaskJobLogController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private TaskJobLogService taskJobLogService;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String index(Model model) {
//        Map<String, String> statusList = codeService.getCodeList("status");
//        model.addAttribute("statusList", statusList);
        return "task/log/index";
    }

    /**
     * 分页查询定时任务执行日志数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    @ResponseBody
    public PageResponse<TaskJobLog> page(PageRequest<TaskJobLog> request) {
        return taskJobLogService.getPageList(request);
    }

    /**
     * 列表查询定时任务执行日志数据
     *
     * @param taskJobLog
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    public Rt<?> list(@RequestBody TaskJobLog taskJobLog) {
        List<TaskJobLog> list = taskJobLogService.getList(taskJobLog);
        return Rt.success(list);
    }

    /**
     * 查询定时任务执行日志详情数据
     *
     * @param taskJobLog
     * @return
     */
    @PostMapping("detail")
    @ResponseBody
    public Rt<?> detail(TaskJobLog taskJobLog) {
        return Rt.success(taskJobLogService.getOneObj(taskJobLog));
    }

    /**
     * 根据主键ID查询定时任务执行日志详情数据
     *
     * @param id 主键ID
     * @return
     */
    @PostMapping("getById")
    @ResponseBody
    public Rt<?> getById(Long id) {
        TaskJobLog taskJobLog = taskJobLogService.getById(id);
        return Rt.success(taskJobLog);
    }

    /**
     * 保存定时任务执行日志数据
     *
     * @param taskJobLog
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public Rt<?> save(@RequestBody TaskJobLog taskJobLog) {
        if (Objects.isNull(taskJobLog.getId())) {
            // 新增
            taskJobLogService.createObj(taskJobLog);
        } else {
            // 更新
            taskJobLogService.updateObj(taskJobLog);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除定时任务执行日志数据
     *
     * @param taskJobLog
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Rt<?> delete(TaskJobLog taskJobLog) {
        taskJobLogService.deleteObj(taskJobLog);
        return Rt.success("删除成功！");
    }

    /**
     * 根据主键ID删除定时任务执行日志数据
     *
     * @param id 主键ID
     * @return
     */
    @GetMapping("deleteById/{id}")
    @ResponseBody
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        taskJobLogService.deleteById(id);
        return Rt.success("删除成功！");
    }
}
